//
//  MyLeave.m
//  SchoolTask
//
//  Created by feel on 16/8/26.
//  Copyright © 2016年 sm. All rights reserved.
//

#import "MyLeave.h"
#import "HZQDatePickerView.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

@interface MyLeave ()<UIActionSheetDelegate,HZQDatePickerViewDelegate,UITextViewDelegate>
{
    HZQDatePickerView *_pikerView;
    NSString *startDate1;
    NSString *endData1;
    NSString *leaveWord;


}
@property (nonatomic,retain) UIView   *reasonView;         //请假类型
@property (nonatomic,retain) UILabel  *reasonLb;           //展示请假类型
@property (nonatomic,retain) UIButton *selectReason;       //选择请假类型按钮
@property (nonatomic,retain) NSString * Actionsheet;//选择的状态


@property (nonatomic,retain) UIButton   *timeInfoButton;       //起止和结束日期
@property (nonatomic,retain) UILabel  *startForDate;       //起始日期展示
//@property (nonatomic,retain) UIButton *seletData;          //选择时间按钮
@property (nonatomic,retain) UILabel  *startForTime;       //开始时间展示
@property (nonatomic,retain) UIButton *seletctTime;        //选择时间按钮
@property (nonatomic,retain) UILabel  *endForDate;         //结束日期展示
//@property (nonatomic,retain) UIButton *endSeletData;       //结束时间按钮
@property (nonatomic,retain) UILabel  *endForTime;         //结束时间展示
@property (nonatomic,retain) UIButton *endSeletctTime;     //结束时间按钮

@property (nonatomic,retain) UIView   *dateNumView;        //请假时间
@property (nonatomic,retain) UITextView *date;            //请假天数

@property (nonatomic,retain) UIView   *reasonInfoView;     //原因详情
@property (nonatomic,retain) UITextView *reasonInfo;       //请假原因详情

@property (nonatomic,retain) UIView   *imageView;          //图片

@property (nonatomic,retain) UIView   *instructionsView;   //批示
@property (nonatomic,retain) UILabel  *teacher;            //老师批示展示
@property (nonatomic,retain) UILabel  *classMonitor;       //班长批示展示

@property (nonatomic,retain) UIButton *commitBt;             //提交按钮

@end

@implementation MyLeave

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:230/255.0 green:235/255.0 blue:235/255.0 alpha:1];
        [self setViewAndController];
    }
    return self;
}

- (void)setViewAndController {
    
    CGFloat h = 736 / kScreenH;
    CGFloat w = 414 / kScreenW;
    
    _reasonView      = [[UIView alloc]initWithFrame:CGRectMake(0, 70/h, kScreenW, 50/h)];
    _timeInfoButton    = [[UIButton alloc]initWithFrame:CGRectMake(0, 130/h, kScreenW, 100/h)];
    _dateNumView     = [[UIView alloc]initWithFrame:CGRectMake(0, 240/h, kScreenW, 50/h)];
    _reasonInfoView  = [[UIView alloc]initWithFrame:CGRectMake(0, 300/h, kScreenW, 150/h)];
    _imageView       = [[UIView alloc]initWithFrame:CGRectMake(0, 460/h, kScreenW, 50/h)];
    _instructionsView = [[UIView alloc]initWithFrame:CGRectMake(0, 520/h, kScreenW, 150/h)];
    
    //_reasonView控件配置
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 15/h, 150/w, 30/h)];
    label.text = @"请假类型";
    label.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];
    label.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];

    _reasonLb = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW - 160/h, 10/w, 150/h, 30/w)];

    if (self.Actionsheet.length == 0)
    {
        _reasonLb.text = @"请选择请假类型";
    }else
    {
        _reasonLb.text = self.Actionsheet;
    }
    
    _reasonLb.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17];
    _reasonLb.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];

    _selectReason = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectReason.frame = CGRectMake(kScreenW - 40/h, 10/w, 30/h, 30/w);
    [_selectReason setBackgroundImage:[UIImage imageNamed:@"下拉按钮图标"] forState:UIControlStateNormal];
    [_selectReason addTarget:self action:@selector(reasonAction) forControlEvents:UIControlEventTouchUpInside];
    [_reasonView addSubview:_reasonLb];
    [_reasonView addSubview:_selectReason];
    [_reasonView addSubview:label];

//_timeInfoView
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, 50/h, kScreenW, 1)];
    line.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    
    //开始时间的相关配置
    UILabel *start = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 10/h, 100/w, 30/h)];
    UILabel *end   = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 60/h, 100/w, 30/h)];
    _startForDate  = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW - 250/w, 10/h, 120/w, 30/h)];
//    _seletData     = [UIButton buttonWithType:UIButtonTypeCustom];
//    _seletData.frame = CGRectMake(kScreenW - 120/w, 10/h, 20/w, 30/h);
    
    _startForTime  = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW - 230/w, 10/h, 190/w, 30/h)];
    _seletctTime     = [UIButton buttonWithType:UIButtonTypeCustom];
    _seletctTime.frame = CGRectMake(kScreenW - 30/w, 10/h, 20/w, 30/h);

//    _startForDate.text = @"2016.12.18";
    _startForDate.text = startDate1;
    _startForDate.font = [UIFont fontWithName:@"MicrosoftYaHei" size:16];
//    _startForTime.text = @"12:30";
//    _startForTime.text = startTime1;
    _startForTime.font = [UIFont fontWithName:@"MicrosoftYaHei" size:16];
    _startForDate.textAlignment = 2;
    _startForTime.textAlignment = 2;
    
    [_seletctTime setBackgroundImage:[UIImage imageNamed:@"下拉按钮图标"] forState:UIControlStateNormal];
    _seletctTime.tag = 101;
    
    [_seletctTime addTarget:self action:@selector(seletDataBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [_seletData setBackgroundImage:[UIImage imageNamed:@"下拉按钮图标"] forState:UIControlStateNormal];
//    _seletData.tag = 101;
//    [_seletData addTarget:self action:@selector(seletDataBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    start.textColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1];
    end.textColor   = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1];
    _startForDate.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    _startForTime.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    
    //结束栏相关控件配置
//    _endForDate  = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW - 200/w, 60/h, 80/w, 30/h)];
//    _endSeletData     = [UIButton buttonWithType:UIButtonTypeCustom];
//    _endSeletData.frame = CGRectMake(kScreenW - 120/w, 60/h, 20/w, 30/h);
    
    _endForTime  = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW - 230/w, 60/h, 190/w, 30/h)];
    _endSeletctTime     = [UIButton buttonWithType:UIButtonTypeCustom];
    _endSeletctTime.frame = CGRectMake(kScreenW - 30/w, 60/h, 20/w, 30/h);
    
//    _endForDate.text = endData1;
    _endForDate.font = [UIFont fontWithName:@"MicrosoftYaHei" size:16/w];

    _endForTime.text = endData1;
    _endForTime.font = [UIFont fontWithName:@"MicrosoftYaHei" size:16/w];

    _endForDate.textAlignment = 2;
    _endForTime.textAlignment = 2;
    
    [_endSeletctTime setBackgroundImage:[UIImage imageNamed:@"下拉按钮图标"] forState:UIControlStateNormal];
    _endSeletctTime.tag = 102;
    [_endSeletctTime addTarget:self action:@selector(seletDataBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    [_endSeletData setBackgroundImage:[UIImage imageNamed:@"下拉按钮图标"] forState:UIControlStateNormal];

    _endForDate.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    _endForTime.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];

    
    [_timeInfoButton addSubview:_startForTime];
//    [_timeInfoButton addSubview:_seletData];
    [_timeInfoButton addSubview:_startForDate];
    [_timeInfoButton addSubview:_seletctTime];
    [_timeInfoButton addSubview:start];
    [_timeInfoButton addSubview:end];
    [_timeInfoButton addSubview:line];
    [_timeInfoButton addSubview:_endForDate];
    [_timeInfoButton addSubview:_endForTime];
//    [_timeInfoButton addSubview:_endSeletData];
    [_timeInfoButton addSubview:_endSeletctTime];

    start.text = @"开始时间";
    start.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];
    end.text = @"结束时间";
    end.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];


//请假天数 dateNumView
    UILabel *dateForNum = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 10/h, 100/w, 30/h)];
    dateForNum.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    dateForNum.text = @"请假天数";
    dateForNum.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];

    _date = [[UITextView alloc]initWithFrame:CGRectMake(150/w, 10/h, 150/w, 30/h)];
//    _date.placeholder = @"请输入请假天数";
    _date.scrollEnabled = NO;    //当文字超过视图的边框时是否允许滑动，默认为“YES”
    _date.editable = YES;        //是否允许编辑内容，默认为“YES”
    _date.delegate = self;       //设置代理方法的实现类

    _date.tag = 10000;
    _date.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];

    if(leaveWord.length == 0)
    {
        _date.text = @"请输入请假天数";//设置显示的文本内容
    }
    else
    {
        _date.textColor = [UIColor blackColor];
        _date.text = leaveWord;//设置显示的文本内容
    }

    
    [_dateNumView addSubview:dateForNum];
    [_dateNumView addSubview:_date];
//请假具体原因 reasonInfoView
    UILabel *reason = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 10/h, 100/w, 30/h)];
    reason.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    reason.text = @"请假事由";
    reason.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];

    _reasonInfo = [[UITextView alloc]initWithFrame:CGRectMake(130/w, 10/h, 200/w, 100/h)];
    _reasonInfo.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    //设置文本框的滚动和边框
    _reasonInfo.scrollEnabled = YES;
    _reasonInfo.delegate = self;
    _reasonInfo.tag = 10001;
    _reasonInfo.layer.borderColor = [[UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1] CGColor];
    _reasonInfo.layer.shouldRasterize = YES;
    _reasonInfo.layer.borderWidth  = 1;
    _reasonInfo.layer.cornerRadius = 5;
    
    if(leaveWord.length == 0)
    {
        _reasonInfo.text = @"请输入请假事由";//设置显示的文本内容
    }
    else
    {
        _reasonInfo.textColor = [UIColor blackColor];
        _reasonInfo.text = leaveWord;//设置显示的文本内容
    }

    
    
    [_reasonInfoView addSubview:reason];
    [_reasonInfoView addSubview:_reasonInfo];
    
//图片 imageView
    UILabel *imageLb = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 10/h, 100/w, 30/h)];
    imageLb.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    imageLb.text = @"图片";
    imageLb.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];

    
    [_imageView addSubview:imageLb];

//instructionsView
    UILabel *instructionsLb = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 10/h, 100/w, 30/h)];
    instructionsLb.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    instructionsLb.text = @"审批人";
    instructionsLb.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];

    
    UILabel *counselor = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 50/h, 80/w, 30/h)];
    counselor.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
    counselor.text = @"辅导员:";
    counselor.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];

    
//    UILabel * classMonitor = [[UILabel alloc]initWithFrame:CGRectMake(200/w, 50/h, 100/w, 30/h)];
//    classMonitor.textColor = [UIColor colorWithRed:87/255.0 green:93/255.0 blue:86/255.0 alpha:1];
//    classMonitor.text = @"班长:";
//    [classMonitor.font fontWithSize:21];
//    classMonitor.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18];
//    classMonitor.font = [UIFont fontWithName:@"MicrosoftYaHei Regular" size:21/w];

    [_instructionsView addSubview:instructionsLb];
    [_instructionsView addSubview:counselor];
//    [_instructionsView addSubview:classMonitor];
    
    //提交按钮配置
    _commitBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _commitBt.frame = CGRectMake(0, kScreenH-100/h, kScreenW, 50/h);
    [_commitBt setTitle:@"提交" forState:UIControlStateNormal];
    _commitBt.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18/w];

    _commitBt.tintColor = [UIColor whiteColor];
    _commitBt.backgroundColor = [UIColor colorWithRed:255/255.0 green:140/255.0 blue:1/255.0 alpha:1];

    _reasonView.backgroundColor   = [UIColor whiteColor];
    _timeInfoButton.backgroundColor = [UIColor whiteColor];
    _dateNumView.backgroundColor  = [UIColor whiteColor];
    _imageView.backgroundColor    = [UIColor whiteColor];
    _instructionsView.backgroundColor = [UIColor whiteColor];
    _reasonInfoView.backgroundColor   = [UIColor whiteColor];
    
    [self addSubview:_reasonView];
    [self addSubview:_timeInfoButton];
    [self addSubview:_dateNumView];
    [self addSubview:_reasonInfoView];
    [self addSubview:_imageView];
    [self addSubview:_instructionsView];
    [self addSubview:_commitBt];
}

#pragma mark - 请假类型-----------
- (void)reasonAction {
    
    NSLog(@"选择请假类型");
    //请假状态
    UIActionSheet *myActionSheet;
    myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"病假", @"事假",@"其他", nil];
    [myActionSheet showInView:self];
    myActionSheet.tag = 20;
    
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 20)
    {
        switch (buttonIndex)
        {
            case 0:
                self.Actionsheet = [NSString stringWithFormat:@"病假"];
                _reasonLb.text = self.Actionsheet;
                break;
                
            case 1:
                self.Actionsheet = [NSString stringWithFormat:@"事假"];
                _reasonLb.text = self.Actionsheet;
                
                break;
            case 2:
                self.Actionsheet = [NSString stringWithFormat:@"其他"];
                _reasonLb.text = self.Actionsheet;
                
                break;
            default:
                break;
        }

    }
}

#pragma mark - 请假日期和时间--------------
-(void)seletDataBtnClick:(UIButton *)sender
{
    if (sender.tag == 101)
    {
        //开始日期
        [self setupDateView:DateTypeOfStart];
        
    }
    else if (sender.tag == 102)
    {
        //结束日期
        [self setupDateView:DateTypeOfEnd];
    }
    
}
//选择日期
- (void)setupDateView:(DateType)type
{
    _pikerView = [HZQDatePickerView instanceDatePickerView];
    _pikerView.frame = CGRectMake(0, 0, kScreenW, kScreenH + 20);
    [_pikerView setBackgroundColor:[UIColor clearColor]];
    _pikerView.delegate = self;
    _pikerView.type = type;
    [_pikerView.datePickerView setMinimumDate:[NSDate date]];

    _pikerView.datePickerView.datePickerMode = UIDatePickerModeDateAndTime;

    
    [self addSubview:_pikerView];
}
- (void)getSelectDate:(NSString *)date type:(DateType)type {
    
    NSLog(@"%d - %@", type, date);
    
    switch (type) {
        case DateTypeOfStart:
            _startForTime.text = [NSString stringWithFormat:@"%@", date];
            startDate1 = _startForTime.text;
            break;
            
        case DateTypeOfEnd:
            _endForTime.text = [NSString stringWithFormat:@"%@", date];
            endData1 = _endForTime.text;
            break;
            
        default:
            break;
    }
}

#pragma mark - 请假天数--------
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if (textView.tag == 10000)
    {
        _date.textColor = [UIColor blackColor];
        if(leaveWord.length == 0)
        {
            _date.text = @"";//设置显示的文本内容
        }
        else
        {
            _date.text = leaveWord;//设置显示的文本内容
        }

    }
    else if (textView.tag == 10001)
    {
        _reasonInfo.textColor = [UIColor blackColor];
        if(leaveWord.length == 0)
        {
            _reasonInfo.text = @"";//设置显示的文本内容
        }
        else
        {
            _reasonInfo.text = leaveWord;//设置显示的文本内容
        }

    }
    
}


@end
